package com.test.qnews.custom;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.test.qnews.adapter.WeatherAdapter;

/**
 * Created by dllo on 16/1/19.
 */
//自定义组件,索引条
public class IndexView extends View {
    public static final String[] WORDS = {"A","B","C","D","E","F","G","H","I"
            ,"J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    private int preHeight = 18;//每一个字母的高度
    private ListView mListView;//我们需要设置索引的ListView
    private Paint mPaint;//画笔
    private int blankHeight;//字母和字母之间间隔空白的高度,留着计算
    private WeatherAdapter weatherAdapter;

    //设置listview,下面要调用的,listview的set方法
    public void setmListView(ExpandableListView mListView) {
        this.mListView = mListView;
        weatherAdapter = (WeatherAdapter) mListView.getExpandableListAdapter();
        //获得listview关联的adapter
    }

    public IndexView(Context context) {
        super(context);
        init();//在构造方法里调用下面写的方法
    }

    public IndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();//在构造方法里调用下面写的方法
    }

    //初始化
    private void init(){
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);//画笔颜色
        mPaint.setTextAlign(Paint.Align.CENTER);//文字的对齐方式是居中
        //将高度从dp转换成px
        preHeight = dpToPx(preHeight,getContext());
        //设置字体大小的时候,需要px
        mPaint.setTextSize(preHeight);
        mPaint.setAntiAlias(true);//抗锯齿
    }

    //在该生命周期中,会测量控件的宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //getMeasuredHeight,获得该组件的高度
        blankHeight = (getMeasuredHeight() - (WORDS.length * preHeight))/ (WORDS.length - 1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < WORDS.length; i++) {
            //在该组件上画文字,第一个参数是横向位置,第二个参数是纵向位置
            canvas.drawText(WORDS[i],getMeasuredWidth() / 2,(i + 1) * (preHeight+blankHeight),mPaint);

        }
    }

    //该方法会处理用户的点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            //当用户按下的时候
            int index = (int) (event.getY()/ (preHeight + blankHeight));
            if (index >= WORDS.length){
                //做一个修正
                index = WORDS.length - 1;
            }else if (index < 0){
                //做修正
                index = 0;
            }
            // 通过字母,获得listview的条数,并滚动到该位置
            int pos = weatherAdapter.getIndexFromString(WORDS[index]);
            if (pos >= 0){
                mListView.setSelection(pos);//listview滚动到pos位置
            }

        }
        return super.onTouchEvent(event);
    }

    //将dp转换成px
    private int dpToPx(int dp,Context context){
        int px = 0;
        //获得屏幕的dpi需要通过Resources下的DisplayMetrics中的densityDpi
        Resources resources = context.getResources();
        //获得屏幕的dpi
        DisplayMetrics metrics = resources.getDisplayMetrics();
        px = (int) (dp * metrics.densityDpi/160f + 0.5f);//0.5f为了四舍五入

        return px;
    }




}